package swnxb.小组合作;

import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;
import swnxb.小组合作.初设计.Vote;
import swnxb.小组合作.初设计.VoteOption;

public class VoteResultAction extends ActionSupport {
	private JFreeChart chart;
	private int voteID;
	
	public int getVoteID() {
		return voteID;
	}

	public void setVoteID(int voteID) {
		this.voteID = voteID;
	}

	public JFreeChart getChart() {
		VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
		VoteOptionDAO voteOptionDAO = 
			VoteOptionDAOFactory.getVoteOptionDAOInstance();

		Vote vote = voteDAO.findVoteById(voteID);
		String voteName = vote.getVoteName();
		

		List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID);
		
		DefaultCategoryDataset dcd = new DefaultCategoryDataset();
		

		for(VoteOption voteOption : voteOptions) {
			dcd.setValue(voteOption.getTicketNum(),"",voteOption.getVoteOptionName());
		}

		JFreeChart chart = ChartFactory.createBarChart3D(
				voteName,
				"投票选项",
				"投票数",
				dcd,
				PlotOrientation.VERTICAL ,
				false, 
				true,
				false);
		
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}
}
